//对axios进行二次封装
import axios from 'axios';
//引入进度条
import nprogress from 'nprogress';
//引入进队条样式
import "nprogress/nprogress.css"
//引入仓库
import store from '@/store';

//1、对axios二次封装
const requests = axios.create({
    //基础路径
    baseURL: '/api',
    //请求的超时时间
    timeout: 5000,
    //携带cookie
    withCredentials: true
})
//2、配置请求拦截器
requests.interceptors.request.use(config => {
    //config内主要是对请求头Header配置,比如添加token
    /*暂时前端不用管token
    let uuidToken = store.state.detail.uuid_token;
    if (uuidToken) {
        config.headers.userTempId = uuidToken
    }
    let token = store.state.user.token;
    if (token) {
        config.headers.token = token
    }
     */
    //进度条开始动
    nprogress.start()
    return config;
})
//3、配置响应拦截器
requests.interceptors.response.use((res) => {
    //成功的回调函数
    //进度条结束
    nprogress.done()
    return res.data;
}, (error) => {
    //失败的回调函数
    console.log("响应失败" + error)
    return Promise.reject(new Error('fail'))
})
//4、对外暴露
export default requests;
